const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function formatTimeTwo (number,format) {
  var formateArr = ['Y','M','D','h','m','s'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1) );
  returnArr.push(formatNumber(data.getDate()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i],returnArr[i]);
  }
  return format;
}
//xxtea 加密解密
function long2str(v,w) {
  var vl = v.length;
  var sl = v[vl-1] & 0xffffffff;
  for (var i = 0;i < vl;i++) {
    v[i] = String.fromCharCode(v[i] & 0xff,
      v[i] >>> 8 & 0xff,
      v[i] >>> 16 & 0xff,
      v[i] >>> 24 & 0xff);
  }
  if(w) {
    return v.join('').substring(0,sl);
  } else {
    return v.join('');
  }
}

function str2long(s,w) {
  var len = s.length;
  var v =[];
  for (var i = 0; i < len; i += 4) {
    v[i >> 2] = s.charCodeAt(i) |
    s.charCodeAt(i + 1) << 8 |
    s.charCodeAt(i + 2) << 16 |
    s.charCodeAt(i +3) << 24;
  }
  if (w) {
    v[v.length] = len;
  }
  return v;
}

function xxtea_encrypt(str, key) {
  if(str == "") {
    return "";
  }
  var v = str2long(str, true);
  var k = str2long(key ,false);
  var n = v.length - 1;

  var z = v[n],
      y = v[0],
      delta = 0x9E3779B9;
      var mx, e, q = Math.floor(6+52 / (n + 1)),
      sum = 0;
      while (q-- > 0) {
        sum = sum +delta & 0xffffffff;
        e = sum >>> 2 & 3;
        for (var p =0; p < n; p++) {
          y = v[p + 1];
          mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
          z = v[p] = v[p] + mx & 0xffffffff;
        }
        y = v[0];
        mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
        z = v[n] = v[n] + mx & 0xffffffff;
      }
      return str2Hex(long2str(v, false));
}

function xxtea_decrypt(str, key) {
  if (str == "") {
    return "";
  }

  str = hex2str(str);
  var v = str2long(str, false);
  var k = str2long(key, false);
  var n = v.length - 1;

  var z = v[n - 1],
    y = v[0],
    delta = 0x9E3779B9;
  var mx, e, q = Math.floor(6 + 52 / (n + 1)),
    sum = q * delta & 0xffffffff;
  while (sum != 0) {
    e = sum >>> 2 & 3;
    for (var p = n; p > 0; p--) {
      z = v[p - 1];
      mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
      y = v[p] = v[p] - mx & 0xffffffff;
    }
    z = v[n];
    mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
    y = v[0] = v[0] - mx & 0xffffffff;
    sum = sum - delta & 0xffffffff;
  }

  return long2str(v, true);
}

function str2Hex(input) {
  var output = "";
  var chr1 = "";
  var i = 0;
  do {
    chr1 = input.charCodeAt(i++).toString(16);
    if (chr1.length == 1) chr1 = "0" + chr1;
    output += chr1;
  } while (i < input.length);
  return output;
}

function hex2str(input) {
  var output = "";
  var i = 0;
  while (i < input.length) {
    var k = parseInt(input.substr(i, 1), 16) << 4 | parseInt(input.substr(++i, 1), 16);
    k = k & 255;
    output += String.fromCharCode(k);
    ++i;
  }
  return output;
}

// base64加密解密
function base64_encode(str) { // 编码，配合encodeURIComponent使用
  str = utf16to8(str);
  var c1, c2, c3;
  var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var i = 0,
    len = str.length,
    strin = '';
  while (i < len) {
    c1 = str.charCodeAt(i++) & 0xff;
    if (i == len) {
      strin += base64EncodeChars.charAt(c1 >> 2);
      strin += base64EncodeChars.charAt((c1 & 0x3) << 4);
      strin += "==";
      break;
    }
    c2 = str.charCodeAt(i++);
    if (i == len) {
      strin += base64EncodeChars.charAt(c1 >> 2);
      strin += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
      strin += base64EncodeChars.charAt((c2 & 0xF) << 2);
      strin += "=";
      break;
    }
    c3 = str.charCodeAt(i++);
    strin += base64EncodeChars.charAt(c1 >> 2);
    strin += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
    strin += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
    strin += base64EncodeChars.charAt(c3 & 0x3F)
  }
  return strin
}

function base64_decode(input) { // 解码，配合decodeURIComponent使用
  var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var output = "";
  var chr1, chr2, chr3;
  var enc1, enc2, enc3, enc4;
  var i = 0;
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  while (i < input.length) {
    enc1 = base64EncodeChars.indexOf(input.charAt(i++));
    enc2 = base64EncodeChars.indexOf(input.charAt(i++));
    enc3 = base64EncodeChars.indexOf(input.charAt(i++));
    enc4 = base64EncodeChars.indexOf(input.charAt(i++));
    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;
    output = output + String.fromCharCode(chr1);
    if (enc3 != 64) {
      output = output + String.fromCharCode(chr2);
    }
    if (enc4 != 64) {
      output = output + String.fromCharCode(chr3);
    }
  }
  return utf8_decode(output);
}

function utf8_decode(utftext) { // utf-8解码
  var string = '';
  let i = 0;
  let c = 0;
  let c1 = 0;
  let c2 = 0;
  while (i < utftext.length) {
    c = utftext.charCodeAt(i);
    if (c < 128) {
      string += String.fromCharCode(c);
      i++;
    } else if ((c > 191) && (c < 224)) {
      c1 = utftext.charCodeAt(i + 1);
      string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
      i += 2;
    } else {
      c1 = utftext.charCodeAt(i + 1);
      c2 = utftext.charCodeAt(i + 2);
      string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
      i += 3;
    }
  }
  return string;
}

function utf16to8(str) {
  var out, i, len, c;
  out = "";
  len = str.length;
  for (i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
      out += str.charAt(i);
    } else if (c > 0x07FF) {
      out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
      out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
      out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
    } else {
      out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
      out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
    }
  }
  return out;
}

module.exports = {
  formatTime: formatTime,
  formatTimeTwo:formatTimeTwo,
  xxtea_encrypt: xxtea_encrypt,
  xxtea_decrypt: xxtea_decrypt,
  base64_encode: base64_encode,
  base64_decode: base64_decode
}
